package com.nari.ies.ne.nw.server.service.fileDataCheck.impl;

import com.nari.ies.ne.nw.server.entity.FileDataCheckEntity;
import com.nari.ies.ne.nw.server.mapper.FileDataCheckMapper;
import com.nari.ies.ne.nw.server.mapper.PlantDataMapper;
import com.nari.ies.ne.nw.server.service.fileDataCheck.IFDJZFileDataCheckService;
import com.nari.ies.ne.nw.server.util.DataUtil;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FDJZFileDataCheckServiceImpl implements IFDJZFileDataCheckService {
    @Autowired
    private PlantDataMapper plantDataMapper;

    @Autowired
    private FileDataCheckMapper fileDataCheckMapper;

    @Override
    public void FDJZFileDataCheck(File file) {
//        System.out.println("数据评分--------------------" + file.getName());
        log.info("数据评分--------------------" + file.getName());
        FileDataCheckEntity fileDataCheckEntity;
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型
        int windSpeedNoData = 0;//测风仪风速无数据
        int windSpeedQualifiedData = 0;//测风仪风速合格数据(有数据且合格)
        int windSpeedUnqualifiedData = 0;//测风仪风速不合格数据(有数据但不合格)
        int windDirectionNoData = 0;//测风仪风向无数据
        int windDirectionQualifiedData = 0;//测风仪风向合格数据(有数据且合格)
        int windDirectionUnqualifiedData = 0;//测风仪风向不合格数据(有数据但不合格)
        int temperatureNoData = 0;//环境温度无数据
        int temperatureQualifiedData = 0;//环境温度合格数据(有数据且合格)
        int temperatureUnqualifiedData = 0;//环境温度不合格数据(有数据但不合格)
        int activePowerNoData = 0;//有功功率无数据
        int activePowerQualifiedData = 0;//有功功率合格数据(有数据且合格)
        int activePowerUnqualifiedData = 0;//有功功率不合格数据(有数据但不合格)
        int reactivePowerNoData = 0;//无功功率无数据
        int reactivePowerQualifiedData = 0;//无功功率合格数据(有数据且合格)
        int reactivePowerUnqualifiedData = 0;//无功功率不合格数据(有数据但不合格)
        int powerFactorNoData = 0;//风机功率因数无数据
        int powerFactorQualifiedData = 0;//风机功率因数合格数据(有数据且合格)
        int powerFactorUnqualifiedData = 0;//风机功率因数不合格数据(有数据但不合格)
        int powerTheoryNoData = 0;//风机有功理论能力无数据
        int powerTheoryQualifiedData = 0;//风机有功理论能力合格数据(有数据且合格)
        int powerTheoryUnqualifiedData = 0;//风机有功理论能力不合格数据(有数据但不合格)
        int fanStatusNoData = 0;//风机状态无数据
        int fanStatusQualifiedData = 0;//风机状态合格数据(有数据且合格)
        int fanStatusUnqualifiedData = 0;//风机状态不合格数据(有数据但不合格)
        int wireNoData = 0;//所属集电线无数据
        int wireQualifiedData = 0;//所属集电线合格数据(有数据且合格)
        int wireUnqualifiedData = 0;//所属集电线不合格数据(有数据但不合格)

        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_FDJZ";//表名
        // 数据计数
        int count = 0;
        //风机数量
        int fansNumber = 0;

        String fileName = file.getName();

        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        //根据CODE查询场站ID
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);

        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 读取到的每一行数据
            String line = "";

            while ((line = br.readLine()) != null) {
                // 预测时间标签行
                if (line.contains("<FDJZ")) {
                    forecastDate = DataUtil.queryTime(line);
                }
                String[] strs = line.split("\t");

                // 数据行
                if (line.contains("#")) {
                    // 数据计数
                    count++;

                    // 测风仪风速
                    if (!" ".equals(strs[3]) && !"null".equals(strs[3]) && !"NULL".equals(strs[3])) {
                        if (Float.parseFloat(strs[3]) >= 0 && Float.parseFloat(strs[3]) < 100) {
                            windSpeedQualifiedData++;
                        }
                    }

                    // 测风仪风向
                    if (!" ".equals(strs[4]) && !"null".equals(strs[4]) && !"NULL".equals(strs[4])) {
                        if (Float.parseFloat(strs[4]) >= 0 && Float.parseFloat(strs[4]) <= 360) {
                            windDirectionQualifiedData++;
                        }
                    }

                    // 环境温度
                    if (!" ".equals(strs[5]) && !"null".equals(strs[5]) && !"NULL".equals(strs[5])) {
                        if (Float.parseFloat(strs[5]) >= -20 && Float.parseFloat(strs[5]) <= 60) {
                            temperatureQualifiedData++;
                        }
                    }

                    // 有功功率
                    if (!" ".equals(strs[20]) && !"null".equals(strs[20]) && !"NULL".equals(strs[20])) {
                        if (Float.parseFloat(strs[20]) >= 0) {
                            activePowerQualifiedData++;
                        }
                    }

                    // 无功功率(无要求)
                    if (!" ".equals(strs[21]) && !"null".equals(strs[21]) && !"NULL".equals(strs[21])) {
                        reactivePowerQualifiedData++;
                    }

                    // 风机功率因数(无要求)
                    if (!" ".equals(strs[22]) && !"null".equals(strs[22]) && !"NULL".equals(strs[22])) {
                        powerFactorQualifiedData++;
                    }

                    // 风机有功理论能力
                    if (!" ".equals(strs[27]) && !"null".equals(strs[27]) && !"NULL".equals(strs[27])) {
                        if (Float.parseFloat(strs[27]) >= 0) {
                            powerTheoryQualifiedData++;
                        }
                    }

                    // 风机状态(1~6)
                    if (!" ".equals(strs[28]) && !"null".equals(strs[28]) && !"NULL".equals(strs[28])) {
                        if (Float.parseFloat(strs[28]) == 1 || Float.parseFloat(strs[28]) == 2
                                || Float.parseFloat(strs[28]) == 3 || Float.parseFloat(strs[28]) == 4
                                || Float.parseFloat(strs[28]) == 5 || Float.parseFloat(strs[28]) == 6
                        ) {
                            fanStatusQualifiedData++;
                        }
                    }

                    // 风机所属集电线(无要求)
                    if (!" ".equals(strs[29]) && !"null".equals(strs[29]) && !"NULL".equals(strs[29])) {
                        wireQualifiedData++;
                    }
                }
            }

            //无数据量
            int noDataNumber = 0;
            //风机数量
            fansNumber = plantDataMapper.selectWindGeneratorNumber(Long.parseLong(plantId));
            System.out.println("风机数量："+fansNumber);
            if (fansNumber >= 0) {
                noDataNumber = Math.max(fansNumber - count, 0);
            }

            // 测风仪风速
            dataType = "windSpeed";
            // 测风仪风速无数据
            windSpeedNoData = noDataNumber;
            // 测风仪风速不合格
            windSpeedUnqualifiedData = count - windSpeedQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, windSpeedNoData, windSpeedQualifiedData, windSpeedUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 测风仪风向
            dataType = "windDirection";
            // 测风仪风向无数据
            windDirectionNoData = noDataNumber;
            // 测风仪风向不合格
            windDirectionUnqualifiedData = count - windDirectionQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, windDirectionNoData, windDirectionQualifiedData, windDirectionUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 环境温度
            dataType = "temperature";
            // 环境温度无数据
            temperatureNoData = noDataNumber;
            // 环境温度不合格
            temperatureUnqualifiedData = count - temperatureQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, temperatureNoData, temperatureQualifiedData, temperatureUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 有功功率
            dataType = "activePower";
            // 有功功率无数据
            activePowerNoData = noDataNumber;
            // 有功功率不合格
            activePowerUnqualifiedData = count - activePowerQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, activePowerNoData, activePowerQualifiedData, activePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 无功功率
            dataType = "reactivePower";
            // 无功功率无数据
            reactivePowerNoData = noDataNumber;
            // 无功功率不合格
            reactivePowerUnqualifiedData = count - reactivePowerQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, reactivePowerNoData, reactivePowerQualifiedData, reactivePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 风机功率因数
            dataType = "powerFactor";
            // 风机功率因数无数据
            powerFactorNoData = noDataNumber;
            // 风机功率因数不合格
            powerFactorUnqualifiedData = count - powerFactorQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, powerFactorNoData, powerFactorQualifiedData, powerFactorUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 风机有功理论能力
            dataType = "powerTheory";
            // 风机有功理论能力无数据
            powerTheoryNoData = noDataNumber;
            // 风机有功理论能力不合格
            powerTheoryUnqualifiedData = count - powerTheoryQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, powerTheoryNoData, powerTheoryQualifiedData, powerTheoryUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 风机状态
            dataType = "fanStatus";
            // 风机状态无数据
            fanStatusNoData = noDataNumber;
            // 风机状态不合格
            fanStatusUnqualifiedData = count - fanStatusQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, fanStatusNoData, fanStatusQualifiedData, fanStatusUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 风机所属集电线
            dataType = "wire";
            // 风机有功理论能力无数据
            wireNoData = noDataNumber;
            // 风机有功理论能力不合格
            wireUnqualifiedData = count - wireQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, wireNoData, wireQualifiedData, wireUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate, tableName);
            if (data == 0) {
                log.info("数据评分插入-------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList, tableName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }
}

